package com.uwlaser.rest;

import com.uwlaser.annotation.Log;
import com.uwlaser.service.ProductCodingRuleService;
import com.uwlaser.service.dto.ProductCodingRuleDto;
import com.uwlaser.service.dto.ProductCodingRuleQueryParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

/**
* @author zk-Qin
* @date 2022-05-11
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "产品编码规则管理")
@RequestMapping("/api/productCodingRule")
public class ProductCodingRuleController {

    private final ProductCodingRuleService productCodingRuleService;

    @GetMapping
    @Log("查询产品编码规则")
    @ApiOperation("查询产品编码规则")
    @PreAuthorize("@el.check('productCodingRule:list')")
    public ResponseEntity query(ProductCodingRuleQueryParam query, Pageable pageable){
        return new ResponseEntity<>(productCodingRuleService.queryAll(query,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增产品编码规则")
    @ApiOperation("新增产品编码规则")
    @PreAuthorize("@el.check('productCodingRule:add')")
    public ResponseEntity create(@Validated @RequestBody ProductCodingRuleDto resources) throws Exception {
        return new ResponseEntity<>(productCodingRuleService.insert(resources)
                , HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改产品编码规则")
    @ApiOperation("修改产品编码规则")
    @PreAuthorize("@el.check('productCodingRule:edit')")
    public ResponseEntity update(@Validated @RequestBody ProductCodingRuleDto resources) throws Exception {
        productCodingRuleService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除产品编码规则")
    @ApiOperation("删除产品编码规则")
    @PreAuthorize("@el.check('productCodingRule:del')")
    public ResponseEntity delete(@RequestBody Set<Integer> ids) {
        productCodingRuleService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /*
    @Log("导出产品编码规则")
    @ApiOperation("导出产品编码规则")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('productCodingRule:list')")
    public void download(HttpServletResponse response, ProductCodingRuleQueryParam query) throws IOException {
        productCodingRuleService.download(productCodingRuleService.queryAll(query), response);
    }*/

}
